package de.sdroege.memorizer;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Memorizer extends Activity {
	private static final int ACTIVITY_FETCH = 0;
	private static final int ACTIVITY_MNG_CBOXES = 1;
	private static final int LOADING_CARDS_DIALOG = 1;

	private CardBoxDbAdapter mDbHelper;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Create / open database
		mDbHelper = new CardBoxDbAdapter(this);
		mDbHelper.open();

		Button btn_learn = (Button) findViewById(R.id.learn);
		Button btn_fetchCards = (Button) findViewById(R.id.fetchCards);
		Button btn_manageCBoxes = (Button) findViewById(R.id.mngCardboxes); 


		btn_learn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(LOADING_CARDS_DIALOG);

			}
		});

		btn_fetchCards.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(LOADING_CARDS_DIALOG);
				//ProgressDialog dialog = ProgressDialog.show(Memorizer.this, "", "Loading. Please wait...", true);
				//ProgressDialog dialog = ProgressDialog.show(Memorizer.this, "Karten werden geladen", "Karteikarten werden geladen", true);
				getCards();
				//dialog.dismiss();
			}
		});

		btn_manageCBoxes.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				manageCBoxes();

			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mainmenu = getMenuInflater();
		mainmenu.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int id = item.getItemId();
		switch(id) {
		case R.id.preferences:
			Intent prefActivity = new Intent(this, MPreferences.class);
			startActivity(prefActivity);
			return true;
		case R.id.quit:
			mDbHelper.close();
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	protected void manageCBoxes() {
		//startActivityForResult(i, ACTIVITY_MNG_CBOXES);		

	}

	private void getCards() {
		Intent ediscioClientService = new Intent(this, EdiscioClient.class);
		startService(ediscioClientService);
//		FetchCards cardfetcher = new FetchCards(this);
//		JSONObject jobj=null;
//		try {
//			jobj = FetchCards.getJSONObject(FetchCards.ediscioCardBoxesURL);
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		try {
//			cardfetcher.saveState(jobj);
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	private void learn() {

	}


	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOADING_CARDS_DIALOG: {
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setTitle("Indeterminate");
			dialog.setMessage("Please wait while loading...");
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		}
		}
		return null;
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}


}